<?php

function lang_app()
{
  $r = ['login', 'register', 'password.reset','password.request'];
  if(request()->get('jp', -1) == 1)
  {
      \App::setLocale('jp');
      session(['jp' => '1']);
  }elseif(request()->get('jp', -1) == 0){
    \App::setLocale('zh-CN');
    session(['jp' => 0]);
  }elseif(request()->get('jp', -1) == -1 && session('jp') == 1 && in_array(\Route::currentRouteName(), $r)){
    //var_dump(\Route::currentRouteName());
    \App::setLocale('jp');
    session(['jp' => '1']);
  }else{
    \App::setLocale('zh-CN');
    session(['jp' => 0]);
  }
}
function translate($object, $filed = '')
{
    if(is_string($object))
    {
        return trans('index.'.$object);
    }
    if(is_array($object))
    {
        $object = (object)$object;
    }
    if($filed == '' || !isset($object->$filed))
    {
        return '';
    }
    if(request()->get('jp', 0) == 1 || session('jp') == 1)
    {
        $newField = 'r'.$filed;
        return isset($object->$newField)?$object->$newField:$object->$filed;
    }else {
        return $object->$filed;
    }
}

function JpUrl($url)
{
    $url = preg_replace('/jp=([\w]+)/', '', $url);
    if(request()->get('jp', 0) == 1  || session('jp') == 1)
    {
        return url(strstr($url, '?')?$url.'&jp=1':$url.'?jp=1');
    }else {
        return url($url);
    }
}
function changeUrl($url = '')
{
	if($url == '')$url = request()->getUri();
	$url = preg_replace('/jp=([\w]+)/', '', $url);
    return url(strstr($url, '?')?$url.'&':$url.'?');


}
function sumTotal($object)
{
    $total = 0;
    foreach ($object as $row)
    {
        if(isset($row->goods_num) && $row->goods_num > 0)$row->number = $row->goods_num;
        $total += $row->goods_attr->price*$row->number;
    }
    return $total;
}

function is_weixin(){
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {  //strpos() 函数查找字符串在另一字符串中第一次出现的位置。
        return true;
    }
    return false;
}
